<?php
/*投诉*/


namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Common\push;


class ComplaintsController extends  Controller{

   private $app_key = '72103a9a13805b96a0ed55b7';        //待发送的应用程序(appKey)，只能填一个。
    private $master_secret ='590ea7bc6b3d916cab6d688a';    //主密码
    private $url = "https://api.jpush.cn/v3/push";      //推送的地址

    private $appios_key = '16f9437a241bd6bf2469b87d';        //待发送的应用程序(appKey)，只能填一个。
    private $masterios_secret ='41c2ef1cfc66d759c8fdee9c';    //主密码
    private $urlios = "https://api.jpush.cn/v3/push";      //推送的地址


    //查看自己的投诉信息
    public function SelectComplaints(Request $request){
        if($request ->isMethod('post')){
            $user_id    = $request ->input('user_id');
           // $village_id = $request ->input('village_id');
            $data       = DB::table('complaints')
                        ->where('user_complaints',$user_id)
                        -> orderBy('complaints_id', 'desc')
                      //  ->where('village_complaints',$village_id)
                        ->get();
            foreach ($data as $value){
                $id                    = $value ->complaints_id;
                $complaints_content    = $value->complaints_content;
                if(empty($value ->complaints_image)) {
                    $complaints_image = [];
                }else{
                    $complaints_image = explode(',',$value->complaints_image);
                }
                $status                = $value ->status;
                $time                  = $value->complaints_create_time;
                $da[]                  = array(
                    'complaints_id'=>$id,
                    'complaints_image'  =>$complaints_image,
                    'complaints_content'=>$complaints_content,
                    'status'=>$status,'time'=>$time);
            }
            if($data == true){
                return json::json('success', '获取自己的投诉信息成功', $da);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',[]);
            }
        }else{
            return json::json('101','提交方式错误','');
        }

    }

    //物业回复
    public function MessageComplaints(Request $request){
        if($request ->isMethod('post')){
            $user_id    = $request ->input('complaints_id');
            $data       = DB::table('user')
                ->select('complaints_content','complaintsreply','work_head','district',
                    'village_name','work_nickname','complaintsreply_create_time','complaints_image')
                ->leftjoin('village','user.user_village','=','village.village_id')
                ->leftjoin('complaints','user.user_id','=','complaints.user_complaints')
                ->leftjoin('complaintsreply','complaints.complaints_id','=','complaintsreply.complaints_complaintsreply')
                ->leftjoin('work','complaintsreply.complaintsreply_property','=','work.work_id')
                ->where('complaints_id',$user_id)
                ->get();
            foreach ($data as $key => $value){
                $complaints_content   = $value ->complaints_content;
                $complaintsreply      = $value ->complaintsreply;
                //$tenement      = $value ->tenement;
                $district      = $value ->district;
                $village_name  = $value ->village_name;
                if(empty($value ->complaints_image)) {
                    $complaints_image = [];
                }else{
                    $complaints_image = explode(',',$value->complaints_image);
                }
              //  $creation      = $value ->creation;
                $complaintsreply_time = $value ->complaintsreply_create_time;
                $arr['complaints_content']  = $complaints_content;
              //  $arr['creation']          = $creation;
                $arr['site']          = $district."-".$village_name;
                $arr['image']         = $complaints_image;
                $arr['list'][]            = array(
                    'head'                =>$value ->work_head,
                    'nickname'            =>$value ->work_nickname,
                    'complaintsreply' =>$complaintsreply,
                    'complaintsreply_time'=>$complaintsreply_time);
            }
          //  $da  = array('bbc' =>$arr);
            if($data){
                return json::json('success', '获取自己的投诉回复信息成功', $arr);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(2));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //添加
    public function AddComplaints(Request $request){
        if($request ->isMethod('post')){
            $user_id      = $request ->input('user_id');
           // $village_id   = $request ->input('village_id');
            $complaints_content = $request ->input('complaints_content');
            $complaints_image   = $request ->input('complaints_image');
            $creation_time      = date('Y-m-d H:i:s');
            $data               = DB::table('complaints')
                ->insert([
                    'user_complaints'   => $user_id,
                   // 'village_complaints' =>$village_id,
                    'complaints_content' => $complaints_content,
                    'complaints_image'    => $complaints_image,
                    'complaints_create_time'       => $creation_time
                ]);
            if($data == true){
               $content   ="您有一条投诉信息";
                /*            json::push($content);
                           $data  = array('content'=>$content,'type'=>1);*/


             //   $pushios   = new push($this ->appios_key,$this ->masterios_secret,$this->urlios);
              //  $pushios ->send_pub1($pubdata->user_village,$content,'7');

                echo json::json('success', '投诉成功', $data);
                try {
                    $pubdata = DB::table('user')->where('user_id', $user_id)->first();
                    $push = new push($this->app_key, $this->master_secret, $this->url);
                    $push->send_pub($pubdata->user_village, $content, '7');
                    $pushios = new push($this->appios_key, $this->masterios_secret, $this->urlios);
                    $pushios->send_pub($pubdata->user_village, $content, '7');
                }catch (\Exception $exception){
                    exit();
                }
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }
    //修改
   /* public function UpdateComplaints(Request $request){
        if($request ->isMethod('post')){
            $user_id      = $request ->input('complaints_id');
            $complaints_content = $request ->input('complaints_content');
            $complaints_image   = $request ->input('complaints_image');
            $position           = $request ->input('position');
            $data               = DB::table('complaints')
                ->where('complaints_id',$user_id)
                ->update([
                    'complaints_content' => $complaints_content,
                    'complaints_image'    => $complaints_image,
                    'position'             => $position,
                ]);
            if($data == true){
                return json::json('success', '投诉成功', '');
            }else{
                return json::json('failure','由于网络或者不正当原因注册失败','');
            }
        }else{
            return json::json('101','提交方式错误','');
        }

    }*/

    //删除
    public function DelComplaints(Request $request){
        if($request ->isMethod('post')){
            $user_id      = $request ->input('complaints_id');
            $data               = DB::table('complaints')
                ->where('complaints_id',$user_id)
               ->delete();
            if($data == true){
                return json::json('success', '投诉删除成功', json::value(1));
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }




}